//
//  OADExpendableCell.m
//  OASDK_Works
//
//  Created by ChenJie on 5/23/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADExpendableCell.h"
#import "OASDK.h"

typedef NS_ENUM(NSUInteger, ComponentId) {
    TitleLabel,
    ContentLabel,
    Separator,
};

@interface OADExpendableCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIView *separator;

@end

@implementation OADExpendableCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.backgroundColor = OAColorString(@"efefef");
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.separator];
        
//        self.titleLabel.backgroundColor = OARandomColor();
//        self.contentLabel.backgroundColor = OARandomColor();
        
//        [self.contentView oa_showBounds];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.titleLabel.frame = OARectWithValue(self.viewModel.oa_layouts[@(TitleLabel)]);
    self.contentLabel.frame = OARectWithValue(self.viewModel.oa_layouts[@(ContentLabel)]);
    self.separator.frame = OARectWithValue(self.viewModel.oa_layouts[@(Separator)]);
    
    self.contentLabel.hidden = !self.viewModel.isExpanded;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
    self.titleLabel.text = nil;
}

#pragma mark - Public methods

#define kMarginV 15
#define kMarginH 15
#define kTitleFont [UIFont systemFontOfSize:16]
#define kContentMarginTop 10
#define kSeparatorMarginTop 10
#define kSeparatorHeight 0.5
#define kSeparatorMarginH 15

+ (CGFloat)oa_heightWithWidth:(CGFloat)cellWidth viewModel:(OADExpendableCellViewModel *)viewModel {
    
//    CGFloat closeHeight = 100;
//    CGFloat cellHeihgt = 0;
    
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    CGFloat topMark = 0;
    
    //
    topMark = kMarginV;
    CGFloat labelWidth = cellWidth - kMarginH * 2;
    CGSize titleSize = [viewModel.title oa_sizeWithWidth:labelWidth font:kTitleFont];
    CGRect titleFrame = CGRectMake(kMarginH, topMark, titleSize.width, titleSize.height);
    layouts[@(TitleLabel)] = OAValueWithRect(titleFrame);
    topMark += titleSize.height;
    
    //
    
    if (viewModel.isExpanded) {
        topMark += kContentMarginTop;
        CGSize contentSize = [viewModel.content oa_sizeWithWidth:labelWidth font:kTitleFont];
        CGRect contentFrame = CGRectMake(kMarginH, topMark, contentSize.width, contentSize.height);
        layouts[@(ContentLabel)] = OAValueWithRect(contentFrame);
        topMark += contentSize.height;
    }
    
    //
    topMark += kMarginV;
//    CGFloat separatorTop = cellHeight - kSeparatorHeight;
    CGFloat separatorWidth = cellWidth - kSeparatorMarginH * 2;
    CGRect separatorFrame = CGRectMake(kSeparatorMarginH, topMark, separatorWidth, kSeparatorHeight);
    layouts[@(Separator)] = OAValueWithRect(separatorFrame);
    
    viewModel.oa_layouts = layouts;
    
    return CGRectGetMaxY(separatorFrame);
}

#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(OADExpendableCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    self.contentLabel.text = viewModel.content;
//    self.contentLabel.hidden = !viewModel.isExpanded;
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.numberOfLines = 0;
})

OAGetLabel(contentLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.numberOfLines = 0;
})

OAGetView(separator, ^(UIView *view){
    view.backgroundColor = OAColorString(@"cccccc");
})

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
